package com.lanou3g.code0418.wait;

/**
 * Created by dllo on 18/4/18.
 */
public class Main {
    public static void main(String[] args) {
        final Object lock = new Object();
        Thread a = new Thread() {
            @Override
            public void run() {
                System.out.println("a线程开始了");
                synchronized (lock) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                System.out.println("a线程结束了");
            }
        };
        a.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("准备唤醒a线程");
        synchronized (lock) {
            lock.notify();
            /*
            wait()和sleep的区别
            wait是object地雷的方法,任何对象都有
            sleep是Thresd的方法
            wait必须写在synchronized代码块中,,并且必须是锁对象调用
            wait()方法,而sleep没有限制(不需要synchronized)
            wait()会释放锁对象(解开锁),而sleep()不会释放锁
            wait()可以被别的线程唤醒,而sleep()需要等待时间结束
            wait是
             */
        }
    }

}
